from random import choice
import time
import string
import test_ps5_ghost

WORDLIST_FILENAME = 'words.txt'
fragment = ' '
global_p = 1

def load_words():
    '''
    Returns a list of valid words. Words are strings of lowercase letters.
    
    Depending on the size of the word list, this function may
    take a while to finish.
    '''
    print "Loading word list from file..."
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r', 0)
    # wordlist: list of strings
    wordlist = []
    for line in inFile:
        wordlist.append(line.strip().lower())
    print "  ", len(wordlist), "words loaded."
    return wordlist

wl = load_words()

def get_letter():
    ''' 
    Prompts the current player to enter a letter.
    Returns: letter (or false)
    '''
    global global_p
    letter = raw_input('player '+ str(global_p)+ ', says a letter: ')
    assert letter.lower() in string.ascii_letters
    print''
    return letter.lower()

def switch_player():
    global global_p
    if global_p == 1: global_p = 2
    else: global_p = 1

def find_index(e,l,h,wl):
    '''search method t() returns index of word that could be formed out of the string 's' provided
    e: string (fragment of word)
    l: low value range of search
    h: high calue range of search
    wl: wordlist
    
    returns:index (or None)
    '''
    s = string.lower(e)
    leng = len(s)
    index = None
    m = l + (h-l)/2
    if (wl[m][:leng] == s): 
        index = m
        return index
        #return True
    if (h-l)<2: 
        if (wl[l][:leng]==s): index = l
        elif(wl[h][:leng]==s): index = h
        else: return -1 #This should be empty
    if(wl[m][:leng]>s):return find_index(e,l,m,wl)
    return find_index(e,m,h,wl)

def search(fragment):
    ''' simple wrapper utility for find_index()'''
    return find_index(fragment , 0, len(wl) - 1, wl)

def display_frag():
    print "Current word fragment: '%s'"%fragment

def display_intro():
    print "Welcome to Ghost!"
    print "Player",global_p,"goes first"
    
def ghost():
    #play_game()
    global fragment
    global global_p
    display_intro()
    display_frag()
    letter = get_letter()
    if letter:fragment = letter
    i = 1
    while i is not -1 and fragment not in wl: 
        display_frag()
        switch_player()
        letter = get_letter()
        fragment += letter
        i = search(fragment.strip())
        #print "VARIABLE i: %d" % i
    #print "game over"
    if i == -1:
        print "Player %s loses, because no word begins with '%s'!" % (global_p,fragment.upper())
        switch_player()
        print "Player %s wins!" % (global_p)
    elif len(fragment)>3:
        print "Player %s loses, because '%s' is a word!" % (global_p,fragment.upper())
        switch_player()
        print "Player %s wins!" % (global_p)
    else: print "'%s' is a word, but it is less than 4 characters!" % fragment.upper()
    print "GAME OVER"
    return


def tests():
    test_ps5_ghost.run_tests()
    
if __name__ == "__main__":
    tests()
    ghost()